﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;
using dodone.DataSetTableAdapters;
using System.Data.SQLite;
using System.Data.Common;

namespace dodone
{
	public enum Menu
	{
		Today,
		Next,
		Someday,
		Logbook,
		Trash
	}

	/// <summary>
	/// Interaction logic for TodoWindow.xaml
	/// </summary>
	public partial class TodoWindow : UserControl
	{
		private Menu selectedMenu = Menu.Next;

		public TodoWindow()
		{
			InitializeComponent();

			lbxTodo.DataContext = Todos;
			TodoView.CustomFilter = "State = 1 AND NOT Trash";

			lbxProject.DataContext = Projects;
		}

		private DataSet.ProjectDataTable projects;
		internal DataSet.ProjectDataTable Projects
		{
			get
			{
				if (projects == null)
				{
					projects = new ProjectTableAdapter().GetData();
				}
				return projects;
			}
		}

		private DataSet.TodoDataTable todos;
		internal DataSet.TodoDataTable Todos
		{
			get
			{
				if (todos == null)
				{
					todos = new TodoTableAdapter().GetData();
				}
				return todos;
			}
		}

		private BindingListCollectionView todoView;
		internal BindingListCollectionView TodoView
		{
			get
			{
				if (todoView == null)
				{
					todoView = (BindingListCollectionView)CollectionViewSource.GetDefaultView(Todos);
				}
				return todoView;
			}
		}

		private void btnAddTodo_Click(object sender, RoutedEventArgs e)
		{
			switch (selectedMenu)
			{
				case Menu.Today:
					Todos.AddTodoRow("", "", false, true, 1, false, DateTime.Now, 0);
					break;
				case Menu.Next:
					Todos.AddTodoRow("", "", false, false, 1, false, DateTime.Now, 0);
					break;
				case Menu.Someday:
					Todos.AddTodoRow("", "", false, false, 2, false, DateTime.Now, 0);
					break;
			}

			lbxTodo.SelectedIndex = TodoView.Count - 1;
		}

		private void btnAddProject_Click(object sender, RoutedEventArgs e)
		{
			Projects.AddProjectRow("New Project");
		}

		void TodoTableAdapter_RowUpdated(object sender, RowUpdatedEventArgs e)
		{
			if (e.StatementType == StatementType.Insert)
			{
				SQLiteCommand todoNewID = new SQLiteCommand("SELECT MAX(ID) from Todo", (SQLiteConnection)e.Command.Connection);
				e.Row["ID"] = Convert.ToInt32(todoNewID.ExecuteScalar());
			}
		}

		void ProjectTableAdapter_RowUpdated(object sender, RowUpdatedEventArgs e)
		{
			if (e.StatementType == StatementType.Insert)
			{
				SQLiteCommand projectNewID = new SQLiteCommand("SELECT MAX(ID) from Project", (SQLiteConnection)e.Command.Connection);
				e.Row["ID"] = Convert.ToInt32(projectNewID.ExecuteScalar());
			}
		}

		private void btnSave_Click(object sender, RoutedEventArgs e)
		{
			TodoTableAdapter todoTableAdapter = new TodoTableAdapter();
			try
			{
				todoTableAdapter.Adapter.RowUpdated += new EventHandler<RowUpdatedEventArgs>(TodoTableAdapter_RowUpdated);
				int rows = todoTableAdapter.Update(Todos);
				MessageBox.Show("Changes saved back to the database, " +
					rows + " row(s) updated.");
			}
			catch (DBConcurrencyException) { }

			ProjectTableAdapter projectTableAdapter = new ProjectTableAdapter();
			try
			{
				projectTableAdapter.Adapter.RowUpdated += new EventHandler<RowUpdatedEventArgs>(ProjectTableAdapter_RowUpdated);
				projectTableAdapter.Update(Projects);
			}
			catch (DBConcurrencyException) { }
		}

		private void btnMod_Click(object sender, RoutedEventArgs e)
		{
		}

		private void btnDel_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				int selectedIndex = lbxTodo.SelectedIndex;
				DataSet.TodoRow selectedRow = (DataSet.TodoRow)((DataRowView)lbxTodo.SelectedItem).Row;

				if (selectedMenu == Menu.Trash)
				{
					MessageBoxResult result;
					result = MessageBox.Show("선택한 Todo를 완전히 삭제 하시겠습니까?", "DoDone", MessageBoxButton.YesNo, MessageBoxImage.Question);

					if (result == MessageBoxResult.Yes)
						selectedRow.Delete();
				}
				else
					selectedRow.Trash = true;

				lbxTodo.SelectedIndex = selectedIndex;
			}
			catch (NullReferenceException) { } // ListBox에서 선택한 아이템이 없는 경우
		}

		private void btnToday_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				DataSet.TodoRow selectedRow = (DataSet.TodoRow)((DataRowView)lbxTodo.SelectedItem).Row;
				selectedRow.Today = !selectedRow.Today;
			}
			catch (NullReferenceException) { } // ListBox에서 선택한 아이템이 없는 경우
		}

		private void mnuToday_Click(object sender, RoutedEventArgs e)
		{
			TodoView.CustomFilter = "Today AND State <= 2 AND NOT Trash";

			selectedMenu = Menu.Today;
		}

		private void mnuNext_Click(object sender, RoutedEventArgs e)
		{
			TodoView.CustomFilter = "State = 1 AND NOT Trash";

			selectedMenu = Menu.Next;
		}

		private void mnuSomeday_Click(object sender, RoutedEventArgs e)
		{
			TodoView.CustomFilter = "State = 2 AND NOT Trash";

			selectedMenu = Menu.Someday;
		}

		private void mnuLogbook_Click(object sender, RoutedEventArgs e)
		{
			TodoView.CustomFilter = "State = 3 AND NOT Trash";

			selectedMenu = Menu.Logbook;
		}

		private void mnuTrash_Click(object sender, RoutedEventArgs e)
		{
			TodoView.CustomFilter = "Trash";

			selectedMenu = Menu.Trash;
		}

		private void btnRestore_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				int selectedIndex = lbxTodo.SelectedIndex;
				DataSet.TodoRow selectedRow = (DataSet.TodoRow)((DataRowView)lbxTodo.SelectedItem).Row;

				if (selectedMenu == Menu.Trash)
					selectedRow.Trash = false;

				lbxTodo.SelectedIndex = selectedIndex;
			}
			catch (NullReferenceException) { } // ListBox에서 선택한 아이템이 없는 경우
		}

		private void lbxTodo_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (expandedTodoItem != null)
			{
				expandedTodoItem.Collapse();
				expandedTodoItem = null;
			}
		}

		private TodoItem expandedTodoItem;

		private void TodoItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
		{
			expandedTodoItem = sender as TodoItem;
		}

	}
}
